package pers.cj.pdos.controller;


import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.cj.pdos.controller.base.BaseController;
import pers.cj.pdos.controller.entity.ResponseData;
import pers.cj.pdos.controller.entity.ResponseEntity;
import pers.cj.pdos.entity.*;
import pers.cj.pdos.service.IBorrowOutBackService;
import pers.cj.pdos.utils.DataUtil;
import pers.cj.pdos.utils.PdosUtils;
import pers.cj.pdos.utils.Utils;
import pers.cj.pdos.vo.BorrowOutBackGoodsVo;
import pers.cj.pdos.vo.BorrowOutBackVo;

import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenj
 * @since 2020-03-25
 */
@Controller
@RequestMapping("/house")
public class BorrowOutBackController extends BaseController {

    @Autowired
    IBorrowOutBackService borrowOutBackService;

    @RequiresPermissions("borrowOut:insert")
    @RequestMapping("/borrowOutBackNewByBorrowOut")
    public String borrowInBackNewByBorrowIn(Model model, String borrowOutId){
        model.addAttribute("borrowOutId", borrowOutId);
        return "/borrowOutBack/borrowOutBackNewByBorrowOut";
    }

    @RequiresPermissions("borrowOut:query")
    @RequestMapping("/borrowOutBackDetail")
    public String borrowOutBackDetail(Model model, String id){
        model.addAttribute("id",id);
        return "/borrowOutBack/borrowOutBackDetail";
    }

    @ResponseBody
    @RequiresPermissions("borrowOut:insert")
    @RequestMapping("/borrowOutBack/add")
    public ResponseEntity add(@RequestBody BorrowOutBackVo borrowOutBackVo){

        Company company = getCompany();
        BusinessSet businessSet = getBusinessSet();

        if (Utils.isEmpty(borrowOutBackVo.getNo())){
            return ResponseEntity.fail("编号不能为空");
        }

        if (Utils.isEmpty(borrowOutBackVo.getBorrowOutBackGoodsVoList())){
            return ResponseEntity.fail("您未选择任何商品");
        }

        if (borrowOutBackVo.getDate() == null){
            return ResponseEntity.fail("业务日期不能为空");
        }

        if (borrowOutBackService.checkNo(borrowOutBackVo.getNo(),company.getId())){
            return ResponseEntity.fail("编号已被使用");
        }


        if (getHouseLocking(borrowOutBackVo.getHouseId())) {
            return ResponseEntity.fail("仓库被锁定，无法归还");
        }

        //准备插入数据库的数据
        BorrowOutBack borrowOutBack = DataUtil.genEntityFromVo(BorrowOutBack.class, borrowOutBackVo);
        DataUtil.processEntity(company,borrowOutBack);
        if (!PdosUtils.houseInOut(businessSet)) {
            borrowOutBack.setStatus(1);
        }else{
            borrowOutBack.setStatus(2);
        }
        borrowOutBack.setMakeUserId(getLoginUserId());
        borrowOutBack.setMakeTime(new Date());
        borrowOutBack.setClose(false);

        List<BorrowOutBackGoods> borrowOutBackGoodsList = new ArrayList<>();
        List<GoodsSn> goodsSnList = new ArrayList<>();
        List<GoodsSnLog> goodsSnLogList = new ArrayList<>();

        Map<String,Object> snInitMap = new HashMap<>();
        snInitMap.put("inDate", new Date());//设置入库日期
        snInitMap.put("status", 1);//设置状态为未出库
        for (BorrowOutBackGoodsVo vo:borrowOutBackVo.getBorrowOutBackGoodsVoList()) {
            BorrowOutBackGoods entity = DataUtil.genEntityFromVo(BorrowOutBackGoods.class, vo);
            DataUtil.processEntity(company,entity);
            entity.setBorrowOutBackId(borrowOutBack.getId());
            borrowOutBackGoodsList.add(entity);

            if (!Utils.isEmpty(vo.getGoodsSnList())){
                for (GoodsSn goodsSn : vo.getGoodsSnList()) {
                    DataUtil.processEntity(company, goodsSn,snInitMap);
                    goodsSnList.add(goodsSn);

                    GoodsSnLog log = new GoodsSnLog();
                    DataUtil.processEntity(company,log);
                    log.setGoodsId(goodsSn.getGoodsId());
                    log.setSn(goodsSn.getSn());
                    log.setType(11);
                    log.setBorrowOutBackGoodsId(entity.getId());
                    goodsSnLogList.add(log);
                }
            }
        }

        borrowOutBackService.insert(businessSet, borrowOutBack, borrowOutBackGoodsList,goodsSnList,goodsSnLogList);

        if (!Utils.isEmpty(goodsSnList) && PdosUtils.houseInOut(businessSet)) {
            //已启用待出库入管理，但是前台传入了sn号，提示
            return ResponseEntity.success("处理成功，管理员在您新增单据过程中启用了出入库管理，录入的sn需要在出库时重新选择");
        }
        return ResponseEntity.success();
    }


    @ResponseBody
    @RequestMapping("/borrowOutBack/getVoById")
    public ResponseData getVoById(String id) {
        return ResponseData.success(borrowOutBackService.getVoById(id));
    }

    @ResponseBody
    @RequestMapping("/borrowOutBack/listVoByBorrowOut")
    public ResponseData listVoByBorrowOut(String borrowOutId) {
        return ResponseData.success(borrowOutBackService.listVoByBorrowOutId(borrowOutId));
    }
}
